#include "orangepch.h"

#include "Shader.h"
#include "Renderer.h"

#include "Platform/OpenGl/OpenGLShader.h"

namespace Orange {
	Shader::~Shader()
	{
	}
	Ref<Shader> Shader::Create(const std::string& filepath)
	{

		switch (Renderer::GetAPI())
		{
		case RendererAPI::API::None: ORANGE_CORE_ASSERT(false, " RenderApi is not support! "); return nullptr;
		case RendererAPI::API::OpenGl: return std::make_shared<OpenGLShader>(filepath);
		}

		ORANGE_CORE_ASSERT(false, " RenderApi unknown! ")
			return nullptr;
	}
	Ref<Shader> Shader::Create(const std::string& name, const std::string& vertexShaderSrc, const std::string& fragmentShaderSrc)
	{

		switch (Renderer::GetAPI())
		{
		case RendererAPI::API::None: ORANGE_CORE_ASSERT(false, " RenderApi is not support! "); return nullptr;
		case RendererAPI::API::OpenGl: return std::make_shared<OpenGLShader>(name,vertexShaderSrc, fragmentShaderSrc);
		}

		ORANGE_CORE_ASSERT(false, "RenderApi unknown! ")
			return nullptr;
	}

	void ShaderLibrary::Add(const Ref<Shader>& shader) {
		std::string name = shader->GetName();
		ORANGE_CORE_ASSERT(m_Shaders.find(name) == m_Shaders.end(), " Shader Already exist!");
		m_Shaders[name] = shader;
	}
	

	void ShaderLibrary::Add(const std::string& name,const Ref<Shader>& shader) {
		
		ORANGE_CORE_ASSERT(m_Shaders.find(name) == m_Shaders.end(), " Shader Already exist!");
		m_Shaders[name] = shader;
	}

	Ref<Shader> ShaderLibrary::Get(const std::string& name) {
		ORANGE_CORE_ASSERT(m_Shaders.find(name) != m_Shaders.end(), " Shader not found!");
		return m_Shaders[name];
	}

	Ref<Shader> ShaderLibrary::Load(const std::string& filepath)
	{
		Ref<Shader> shader = Shader::Create(filepath);
		Add(shader);
		return shader;
	}
	Ref<Shader> ShaderLibrary::Load(const std::string& name, const std::string& filepath)
	{
		Ref<Shader> shader = Shader::Create(filepath);
		Add(name,shader);
		return shader;
	}
}